package gd.qaq.blue.powerparticle;

import gd.qaq.blue.powerparticle.commands.PowerParticleCommands;
import gd.qaq.blue.powerparticle.listener.PowerParticleListener;
import gd.qaq.blue.powerparticle.utils.Particles;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public final class PowerParticle extends JavaPlugin {
    public static PowerParticle plugin;
    public static FileConfiguration config;

    @Override
    public void onEnable() {
        // Plugin startup logic
        plugin = this;
        Bukkit.getServer().getPluginManager().registerEvents(new PowerParticleListener(this), this);
        Bukkit.getPluginCommand("powerparticle").setExecutor(new PowerParticleCommands());
        loadDatas();
        this.getLogger().info("" +
                "\n\t\t\t§a§l-----------------------------------" +
                "\n\t\t\t§bPowerParticle插件载入成功!" +
                "\n\t\t\t§b感谢你的支持! " +
                "\n\t\t\t§b插件有问题请联系§eQQ862899738" +
                "\n\t\t\t§b作者: Blue 接插件定制 客户端定制" +
                "\n\t\t\t§a§l-----------------------------------");
    }

    private void loadDatas() {
        if (!this.getDataFolder().exists()) {
            this.getDataFolder().mkdirs();
            this.saveDefaultConfig();
        }
        config = this.getConfig();
    }

    public void reload(CommandSender p) {
        if (p.isOp()) {
            try {
                this.reloadConfig();
                config = this.getConfig();
                for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()) {
                    if (Particles.playersParticleTask.containsKey(onlinePlayer.getUniqueId().toString())) {
                        Particles.stopParticle(onlinePlayer);
                    }
                }
                success(p, "插件重载成功");
            } catch (Exception e) {
                e.printStackTrace();
                error(p, "插件重载失败!");
            }
        }
    }

    public static String colorFormat(String str) {
        return str.replace("&", "§");
    }

    public static void success(CommandSender p, String str) {
        p.sendMessage("§f[§a提示§f]§a" + colorFormat(str));
    }

    public static void error(CommandSender p, String str) {
        p.sendMessage("§f[§c提示§f]§c" + colorFormat(str));
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
